package www.openteam.com.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.base.mBaseFragment;
import www.openteam.com.bean.AppAuthority;
import www.openteam.com.bean.OTBusiness;
import www.openteam.com.bean.OTPolicy;
import www.openteam.com.bean.OTPrdctnMngmnt;
import www.openteam.com.bean.QualityCtrl;
import www.openteam.com.bean.RealTime;
import www.openteam.com.contract.FAuthorityContract;
import www.openteam.com.presenter.FAuthorityPresenter;
import www.openteam.com.tool.ToastUtil;

/**
 * Created by kylin on 2016/8/4.
 */
public class HomeFragmet extends
        mBaseFragment<FAuthorityPresenter>
        implements FAuthorityContract.FAuthorityView
{

    private static final String TAG = HomeFragmet.class.getSimpleName() ;
    private String mParam;
    @Bind(R.id.gview)
    public GridView gridView;
    public List  data_list;
    public List<String>  a_list;
    public HomeFragmet(){}
    private RealTime[] realTime;
    private OTBusiness[] business;
    private OTPolicy[] policy;
    private OTPrdctnMngmnt[] prdctnMngmnts;
    private QualityCtrl[] qualityCtrls;
    String mTitle;
    int id;
    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null){
            id = getArguments().getInt("id");
            switch(id){
                case 3:
                    Log.d(TAG, "onCreate: 0");
                    realTime= (RealTime[]) getArguments().getSerializable("realTime");
                    break;
                case 2:
                    Log.d(TAG, "onCreate: 2");
                    business = (OTBusiness[]) getArguments().getSerializable("business");
                    break;
                case 1:
                    Log.d(TAG, "onCreate: 1");
                    policy = (OTPolicy[]) getArguments().getSerializable("policy");
                    break;
                case 0:
                    Log.d(TAG, "onCreate: 3");
                    prdctnMngmnts = (OTPrdctnMngmnt[]) getArguments().getSerializable("prdctnMngmnts");
                    break;
                case 4:
                    Log.d(TAG, "onCreate: 4");
                    qualityCtrls = (QualityCtrl[]) getArguments().getSerializable("qualityCtrls");
                    break;
            }
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
        mPresenter = loadPresenter();
        if (mPresenter != null)
            mPresenter.attachView(this);
        return inflater.inflate(R.layout.fragment_home,container,false);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initView() {
        if(TextUtils.isEmpty(mParam)){
        }else {
        }
    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected FAuthorityPresenter loadPresenter() {
        return new FAuthorityPresenter();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,view);

        //新建List
        data_list = new ArrayList<Map<String, Integer>>();
        a_list =new ArrayList();
        //获取数据
        switch (DetailedActivity.FACTORY){
            //"E_HZ_RB";//ZZ//BASE
            case "E_HZ_RB":
                getHZRBData(savedInstanceState);
                break;
            case "ZZ":
                getHZRBData(savedInstanceState);
                break;
            case "BASE":
                getData(savedInstanceState);
                break;
        }

//        //配置适配器
        gridView.setAdapter(new MyGridAdpter(this.getActivity().getApplicationContext(),data_list));

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    TextView title= (TextView) view.findViewById(R.id.tab_title);
                    mTitle=    title.getText().toString();
//                     Log.d("HF",mTitle);3
                SharedPreferences getMySetting = getContext().getSharedPreferences("mySetting", getContext().MODE_PRIVATE);
                    Log.d(TAG, "onItemClick: "+a_list.get(position)+":"+getMySetting.getInt("mId",0));
                mPresenter.checkAuthority(a_list.get(position),getMySetting.getInt("mId",0));
//                a_list.get(position),getMySetting.getInt("mId",0)

            }
        });
        initView();
    }

    private List<Map<String, Object>> getHZRBData(Bundle savedInstanceState) {
        if (getArguments() != null){

            id = getArguments().getInt("id");
            switch(id){
                case 0:
                    realTime= (RealTime[]) getArguments().getSerializable("realTime");
                    for(int i=0;i<realTime.length;i++){
                        Map<String, Integer> map = new HashMap<String, Integer>();
                        map.put("image", realTime[i].getResIcon());
                        map.put("text", realTime[i].getResName());
                        data_list.add(map);
                        a_list.add(i,realTime[i].getAppAuthority());
                    }
                    return data_list;
                case 2:
                    business = (OTBusiness[]) getArguments().getSerializable("business");
                    for(int i=0;i<business.length;i++){
                        Map<String, Integer> map = new HashMap<String, Integer>();
                        map.put("image", business[i].getResIcon());
                        map.put("text", business[i].getResName());
                        data_list.add(map);
                        a_list.add(i,business[i].getAppAuthority());
                    }

                    return data_list;
                case 1:
                    policy = (OTPolicy[]) getArguments().getSerializable("policy");
                    for(int i=0;i<policy.length;i++){
                        Map<String, Integer> map = new HashMap<String, Integer>();
                        map.put("image", policy[i].getResIcon());
                        map.put("text", policy[i].getResName());
                        data_list.add(map);
                        a_list.add(i,policy[i].getAppAuthority());
                    }
                    return data_list;
                case 3:
                    prdctnMngmnts = (OTPrdctnMngmnt[])  getArguments().getSerializable("prdctnMngmnts");
                    for(int i=0;i<prdctnMngmnts.length;i++){
                        Map<String, Integer> map = new HashMap<String, Integer>();
                        map.put("image", prdctnMngmnts[i].getResIcon());
                        map.put("text", prdctnMngmnts[i].getResName());
                        data_list.add(map);
                        a_list.add(i,prdctnMngmnts[i].getAppAuthority());
                    }
                    return data_list;
                case 4:
                    qualityCtrls = (QualityCtrl[])  getArguments().getSerializable("qualityCtrls");
                    for(int i=0;i<qualityCtrls.length;i++){
                        Map<String, Integer> map = new HashMap<String, Integer>();
                        map.put("image", qualityCtrls[i].getResIcon());
                        map.put("text", qualityCtrls[i].getResName());
                        data_list.add(map);
                        a_list.add(i,qualityCtrls[i].getAppAuthority());
                    }
                    return data_list;
            }
        }
        return null;
    }


    public List<Map<String, Object>> getData(Bundle bundle){

        if (getArguments() != null){

            id = getArguments().getInt("id");
            switch(id){
                case 0:
                    realTime= (RealTime[]) getArguments().getSerializable("realTime");
                    for(int i=0;i<realTime.length;i++){
                        Map<String, Integer> map = new HashMap<String, Integer>();
                        map.put("image", realTime[i].getResIcon());
                        map.put("text", realTime[i].getResName());
                        data_list.add(map);
                        a_list.add(i,realTime[i].getAppAuthority());
                    }
                    return data_list;
                case 2:
                    business = (OTBusiness[]) getArguments().getSerializable("business");
                    for(int i=0;i<business.length;i++){
                        Map<String, Integer> map = new HashMap<String, Integer>();
                        map.put("image", business[i].getResIcon());
                        map.put("text", business[i].getResName());
                        data_list.add(map);
                        a_list.add(i,business[i].getAppAuthority());
                    }

                    return data_list;
                case 1:
                    policy = (OTPolicy[]) getArguments().getSerializable("policy");
                    for(int i=0;i<policy.length;i++){
                        Map<String, Integer> map = new HashMap<String, Integer>();
                        map.put("image", policy[i].getResIcon());
                        map.put("text", policy[i].getResName());
                        data_list.add(map);
                        a_list.add(i,policy[i].getAppAuthority());
                    }
                    return data_list;
                case 3:
                    prdctnMngmnts = (OTPrdctnMngmnt[])  getArguments().getSerializable("prdctnMngmnts");
                    for(int i=0;i<prdctnMngmnts.length;i++){
                        Map<String, Integer> map = new HashMap<String, Integer>();
                        map.put("image", prdctnMngmnts[i].getResIcon());
                        map.put("text", prdctnMngmnts[i].getResName());
                        data_list.add(map);
                        a_list.add(i,prdctnMngmnts[i].getAppAuthority());
                    }
                    return data_list;
                case 4:
                    qualityCtrls = (QualityCtrl[])  getArguments().getSerializable("qualityCtrls");
                    for(int i=0;i<qualityCtrls.length;i++){
                        Map<String, Integer> map = new HashMap<String, Integer>();
                        map.put("image", qualityCtrls[i].getResIcon());
                        map.put("text", qualityCtrls[i].getResName());
                        data_list.add(map);
                        a_list.add(i,qualityCtrls[i].getAppAuthority());
                    }
                    return data_list;
            }
        }
        return null;
    }



    @Override
    public void onRefresh() {

    }

    @Override
    public void FAuthoritySuccess(AppAuthority appAuthority) {
        editor.putInt("groupAuthority",appAuthority.getData().get(0).getBrdGrp());
        if (appAuthority.getData().get(0).getChk1()==1) {
            editor.putBoolean("chk",true );
            Log.d(TAG, "getJMark: "+true+appAuthority.getData().get(0).getBrd1()+":"+appAuthority.getData().get(0).getChk1());
        }else {
            editor.putBoolean("chk",false);
            Log.d(TAG, "getJMark: "+false+appAuthority.getData().get(0).getBrd1()+":"+appAuthority.getData().get(0).getChk1());
        }
        editor.commit();
        Intent intent = new Intent(getActivity(),DetailedActivity.class);
        Bundle bundle =new Bundle() ;
        bundle.putString("title", mTitle);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void FAuthorityFail(String failMsg) {
        ToastUtil.showToast(getContext(),failMsg);
    }

    class MyGridAdpter extends BaseAdapter{

        private Context mContext;
        private List<Map<String, Integer>> mList;
        MyGridAdpter(Context mContext ,List<Map<String, Integer>> mList) {
            this.mContext = mContext;
            this.mList=mList;
        }


        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater _LayoutInflater=LayoutInflater.from(mContext);
            convertView=_LayoutInflater.inflate(R.layout.tab_indicator, null);
            if(convertView!=null) {

                TextView title = (TextView) convertView.findViewById(R.id.tab_title);
                Drawable drawable = getResources().getDrawable(mList.get(position).get("image"));
                title.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
                        null);


                title.setText(getString(mList.get(position).get("text")));


            }
            return convertView;

        }
    }


}
